﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Common;
using RegisterationCommon.Student;
using System.Data;

namespace RegisterationSystem.Space_Student
{
    public partial class registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["zhuce"] == null)
                {
                    Response.Redirect("../index.aspx");
                }
                if (SysSet.IsStopSignUp())
                {
                    Response.Redirect("../index.aspx");
                }
                Bind();
            }
            //CommunityBond();
        }
        void Bind()
        {
            ////绑定公告
            //Repeater2.DataSource = DBAction.GetDTFromSQL("select top 2 * from Notice where (Recrive=0 or Recrive=2) and Status=0 order by id desc");
            //Repeater2.DataBind();
        }
        public void CommunityBond()
        {
            string schoolID = "var tschoolID=[";
            DataTable dt3 = DBAction.GetDTFromSQL("select * from Community");
            for (int n = 0; n < dt3.Rows.Count; n++)
            {
                if (n != dt3.Rows.Count - 1)
                {
                    schoolID = schoolID + "['" + dt3.Rows[n]["ID"].ToString() + "'" + ",'" + dt3.Rows[n]["SclID"].ToString() + "'" + "],";
                }
                else
                {
                    schoolID = schoolID + "['" + dt3.Rows[n]["ID"].ToString() + "'" + ",'" + dt3.Rows[n]["SclID"].ToString() + "'" + "]";
                }
            }
            schoolID = schoolID + "];";

            string parentID = "0";
            string jsStart = "<script>";
            string jsEnd = "</script>";
            string parentJs = "var tparent=[";
            string childJs = "var tchilds=[";
            string childJs1 = "var tchilds1=[";
            DataTable dt = DBAction.GetDTFromSQL("select * from Community where pid=" + parentID);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i != dt.Rows.Count - 1)
                {
                    parentJs = parentJs + "['" + dt.Rows[i]["ID"].ToString() + "'" + ",'" + dt.Rows[i]["Name"].ToString() + "'" + "],";
                    DataTable dt1 = DBAction.GetDTFromSQL("select * from Community where pid=" + dt.Rows[i]["ID"].ToString());
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        childJs = childJs + "['" + dt1.Rows[j]["ID"].ToString() + "'" + ",'" + dt1.Rows[j]["Name"].ToString() + "'" + ",'" + dt1.Rows[j]["Pid"].ToString() + "'" + "],";
                        
                        //第三级
                        DataTable dtt1 = DBAction.GetDTFromSQL("select * from Community where pid=" + dt1.Rows[j]["ID"].ToString());
                        for (int k = 0; k < dtt1.Rows.Count; k++)
                        {
                            childJs1 = childJs1 + "['" + dtt1.Rows[k]["ID"].ToString() + "'" + ",'" + dtt1.Rows[k]["Name"].ToString() + "'" + ",'" + dtt1.Rows[k]["Pid"].ToString() + "'" + "],";

                        }
                        //end
                    }
                }
                else
                {
                    parentJs = parentJs + "['" + dt.Rows[i]["ID"].ToString() + "'" + ",'" + dt.Rows[i]["Name"].ToString() + "'" + "]";
                    DataTable dt2 = DBAction.GetDTFromSQL("select * from Community where pid=" + dt.Rows[i]["ID"].ToString());
                    for (int k = 0; k < dt2.Rows.Count; k++)
                    {
                        if (k != dt2.Rows.Count - 1)
                        {
                            childJs = childJs + "['" + dt2.Rows[k]["ID"].ToString() + "'" + ",'" + dt2.Rows[k]["Name"].ToString() + "'" + ",'" + dt2.Rows[k]["Pid"].ToString() + "'" + "],";

                            //第三级
                            DataTable dtt1 = DBAction.GetDTFromSQL("select * from Community where pid=" + dt2.Rows[k]["ID"].ToString());

                            for (int n = 0; n < dtt1.Rows.Count; n++)
                            {
                                childJs1 = childJs1 + "['" + dtt1.Rows[n]["ID"].ToString() + "'" + ",'" + dtt1.Rows[n]["Name"].ToString() + "'" + ",'" + dtt1.Rows[n]["Pid"].ToString() + "'" + "],";

                            }
                            //end
                        }
                        else
                        {
                            childJs = childJs + "['" + dt2.Rows[k]["ID"].ToString() + "'" + ",'" + dt2.Rows[k]["Name"].ToString() + "'" + ",'" + dt2.Rows[k]["Pid"].ToString() + "'" + "]";
                            //第三级
                            DataTable dtt2 = DBAction.GetDTFromSQL("select * from Community where pid=" + dt2.Rows[k]["ID"].ToString());
                            for (int m = 0; m < dtt2.Rows.Count; m++)
                            {
                                if (m != dtt2.Rows.Count - 1)
                                {
                                    childJs1 = childJs1 + "['" + dtt2.Rows[m]["ID"].ToString() + "'" + ",'" + dtt2.Rows[m]["Name"].ToString() + "'" + ",'" + dtt2.Rows[m]["Pid"].ToString() + "'" + "],";
                                }
                                else
                                {
                                    childJs1 = childJs1 + "['" + dtt2.Rows[m]["ID"].ToString() + "'" + ",'" + dtt2.Rows[m]["Name"].ToString() + "'" + ",'" + dtt2.Rows[m]["Pid"].ToString() + "'" + "]";
                                }
                            }
                            //end

                        }
                    }
                }
            }
            parentJs = parentJs + "];";
            if (childJs.Substring(childJs.Length - 1, 1) == ",")
            {
                childJs = childJs.Substring(0, childJs.Length - 1);
            }
            childJs = childJs + "];";

            if (childJs1.Substring(childJs1.Length - 1, 1) == ",")
            {
                childJs1 = childJs1.Substring(0, childJs1.Length - 1);
            }
            childJs1 = childJs1 + "];";

            //string callJS = "for(var i=0;i<tparent.length;i++){";
            //callJS += "$(\"#scommunity\").append(\"<option value='\" + tparent[i][0]+ \"'>\" + tparent[i][1] + \"</option>\");";
            //callJS += "}";
            //callJS += "setChildNode(1,tchilds);plasschoolload(tschoolID);";
            //callJS += "$(\"#scommunity\").change(function(obj){ $(\"#xcommunity\").html('');setChildNode($(this).val(),tchilds);plasschooldialog(tschoolID);});";
            //callJS += "function setChildNode(parentID,childs){ for(var j=0;j<childs.length;j++){ if(childs[j][2]==parentID)	{";
            //callJS += "$(\"#xcommunity\").append(\"<option value='\" + childs[j][0]+ \"'>\" + childs[j][1] + \"</option>\");}}}";
            //callJS += "$(\"#xcommunity\").change(function(obj){plasschoolIDValue(tschoolID);});";
            //cpJS2.Text = jsStart + "$(function () {" + schoolID + parentJs + childJs + callJS + "});" + jsEnd;

            string callJS = "for(var i=0;i<tparent.length;i++){";
            callJS += "$(\"#pcommunity\").append(\"<option value='\" + tparent[i][0]+ \"'>\" + tparent[i][1] + \"</option>\");";
            callJS += "}";
            callJS += "setChildNode(1,tchilds);plasschoolload(tschoolID);";//页面初始赋值
            callJS += "$(\"#pcommunity\").change(function(obj){ $(\"#scommunity\").html('');setChildNode($(this).val(),tchilds);plasschoolload(tschoolID);});";//进城
            
            callJS += "function setChildNode(parentID,childs){ for(var j=0;j<childs.length;j++){ if(childs[j][2]==parentID)	{";
            callJS += "$(\"#scommunity\").append(\"<option value='\" + childs[j][0]+ \"'>\" + childs[j][1] + \"</option>\");}}}setChild1Node(4,tchilds1);";
            callJS += "$(\"#scommunity\").change(function(obj){$(\"#xcommunity\").html('');setChild1Node($(this).val(),tchilds1);plasschoolIDValue1(tschoolID);});";
            //社区变化


            callJS += "function setChild1Node(parent1ID,childs){ for(var j=0;j<childs.length;j++){ if(childs[j][2]==parent1ID)	{";
            callJS += "$(\"#xcommunity\").append(\"<option value='\" + childs[j][0]+ \"'>\" + childs[j][1] + \"</option>\");}}}";

            callJS += "$(\"#xcommunity\").change(function(obj){plasschoolIDValue(tschoolID);});";//小区变化
            cpJS2.Text = jsStart + "$(function () {" + schoolID + parentJs + childJs + childJs1 + callJS + "});" + jsEnd;


        }
        //添加
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from Students where IDCardNumber='" + txtIDCardNumber.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                //已登记
                Message.Send(10011);
                return;
            }
            StudentInfo.Current.TrueName = txtname.Value.Trim();//学生姓名
            StudentInfo.Current.IDCardNumber = txtIDCardNumber.Value.Trim();//学生身份证
            StudentInfo.Current.Sex = ddlsex.SelectedValue == "0" ? false : true;//性别0男1女
            //StudentInfo.Current.Hobby = txtHouseAreaInfo.Value;//兴趣爱好
            StudentInfo.Current.FirstGuardianName = txtFirstGuardianName.Value;//第一监护人姓名
            StudentInfo.Current.FirstGuardianUnit = txtFirstGuardianUnit.Value;//第一监护人工作单位
            StudentInfo.Current.FirstGuardianPhone = txtFirstGuardianPhone.Value;//第一监护人联系电话
            StudentInfo.Current.FirstRelationship = txtFirstRelationship.Value;  //第一监护人和他的关系
            StudentInfo.Current.SecGuardianName = txtSecGuardianName.Value;//第二监护人姓名
            StudentInfo.Current.SecGuardianUnit = txtSecGuardianUnit.Value;//第二监护人工作单位
            StudentInfo.Current.SecGuardianPhone = txtSecGuardianPhone.Value;//第二监护人联系电话
            StudentInfo.Current.SecRelationship = txtSecRelationship.Value;  //第二监护人和他的关系
            string a = hidstuid.Value;
            StudentInfo.Current.SchID = Convert.ToInt32(hidstuid.Value);//所属学校
            StudentInfo.Current.Status = Convert.ToInt32(hidstuid.Value) == 0 ? 0 : 1;//信息状态(0未分配1已分配2初审通过3初审不通过)
            StudentInfo.Current.RegisterTime = DateTime.Now;//登记时间
            StudentInfo.Current.PhoneStatusText = "";//电话回访反馈说明
            StudentInfo.Current.PhoneStatus = false;//电话回访状态(0未回访1回访)
            StudentInfo.Current.IsDelete = false;//是否删除(0未删除 1删除)
            StudentInfo.Current.IsRecord = false;//是否代录
            string NewPwd = StudentInfo.Current.IDCardNumber.Substring(6, 8);//截取身份证生日作为密码
            StudentInfo.Current.LoginPswd = BaseLibrary.ConvertToMD5(NewPwd.ToLower());//保存密码
            //获取身份证上的时间 进行比较 是否足龄
            string stuDate = StudentInfo.Current.IDCardNumber.Substring(6, 4) + "-" + StudentInfo.Current.IDCardNumber.Substring(10, 2) + "-" + StudentInfo.Current.IDCardNumber.Substring(12, 2);
            
            StudentInfo.Current.Birthday = DateTime.Parse(stuDate); //出生年月
       
            //足龄条件
            string yea = (DateTime.Now.Year - 6) + "-09-01";
          
            //身份证时间和足龄条件进行对比
            int isyea = DateTime.Compare(Convert.ToDateTime(yea), Convert.ToDateTime(stuDate));


            StudentInfo.Current.IsMatured = isyea > 0 ? true : false;//是否足龄


            StudentInfo.Current.PreschoolsID = Convert.ToInt32(Preschools.Value);




            //if (isyea > 0)
            //{

            //}
            //else
            //{
            //    return;//不足龄就不进行信息录入
            //}

            StudentInfo.Current.HouseAreaID = Convert.ToInt32(hidhous.Value);   //户口所在地(0经济技术开发区1市内其它区2省内市外、外省外市)
            StudentInfo.Current.ScommAreaID = Convert.ToInt32(hidsid.Value);   //住房社区
            StudentInfo.Current.XcommAreaID = Convert.ToInt32(hidxid.Value);   //住房小区
            StudentInfo.Current.LevelNum = 0;   //住房社区
            StudentInfo.Current.ScommName = hidsname.Value;//社区名称
            StudentInfo.Current.XcommName = hidxname.Value;//小区名称
            StudentInfo.Current.SignUpNum = "42011111";//登记单号
            //StudentInfo.Current.HouseNumber = txtHouseNumber.Value;//门牌号
            StudentInfo.Current.HouseNumber = txtHouseNumber.Value ;//门牌号,如：M-M-M

            StudentInfo.Current.HouseCard = txtHouseCard.Value;//房产号
            StudentInfo.Current.Level = -1;//住房社区
            StudentInfo.Current.Adjustment = "";//是否服从调剂
            StudentInfo.Current.HouseIngoingTime = ddlHouseIngoingTime.Value;//户口迁入时间
            StudentInfo.Current.HousingOccupancyTime = HousingOccupancyTime.Value;//住房入住时间
            StudentInfo.Current.PermanentAddress = txtPermanentAddress.Value;//户籍地址
            StudentInfo.Current.ServicesDivision = 0;// 是否进行了服务划分（0，没有；1划分了）

            StudentInfo.Current.IsSelf = Convert.ToInt32(radio.Value);//是否为自有房产（0是1不是）
            StudentInfo.Current.Ext1 = txt.Value;//单独存储门栋，方便以后的服务划分，按照门栋
            StudentInfo.Current.Ext2 = "";
            StudentInfo.Current.Ext3 = "1";//1不是代录
            StudentInfo.Current.Ext4 = "";//0为特殊人才

            StudentInfo.Current.pianid = Convert.ToInt32(pianid.Value);//片区id
            StudentInfo.Current.pianname = pianname.Value;//片区名称

            if (StudentInfo.Current.SchID == 3 || StudentInfo.Current.SchID == 6 || StudentInfo.Current.SchID == 9 || StudentInfo.Current.SchID == 10)
            {
                StudentInfo.Current.Ext5 = "0";//进城务工
            }

            else
            {
                StudentInfo.Current.Ext5 = "";//
            }
            switch (StudentInfo.Current.HouseAreaID)
            {
                case 0:
                    StudentInfo.Current.Level = 0;//学生等级
                    break;
                case 1:
                    StudentInfo.Current.Level = 1;//学生等级
                    break;
                case 2:
                    StudentInfo.Current.Level = 1;//学生等级
                    break;
                default:
                    StudentInfo.Current.Level = 0;//学生等级
                    break;
            }
            if (StudentInfo.Current.ScommName == "其他" && StudentInfo.Current.HouseAreaID == 2)
            {
                StudentInfo.Current.Level = 2;
                if (hidAdjustment.Value.ToString().Trim()=="0")
                {
                    StudentInfo.Current.Adjustment = "服从";
                }
                else
                {
                    StudentInfo.Current.Adjustment =hidAdjustment.Value.ToString().Trim();
                }
            }
            //市内区外 有没有房产的
            bool isqu = false;
            //if (StudentInfo.Current.SchID == 0 && StudentInfo.Current.HouseAreaID == 1)//就近读书
            if (StudentInfo.Current.IsSelf == 1 && StudentInfo.Current.HouseAreaID == 1)//就近读书租赁房
            {
                isqu = true;
            }
            if (StudentInfo.Current.ScommName == "其他")
            {
                StudentInfo.Current.ServicesDivision = 1;
            }
            if (StudentInfo.Current.AddNewStudentInfo())
            {
                Server.Transfer("messageshow.aspx?idc=" + StudentInfo.Current.IDCardNumber + "&pwd=" + NewPwd + "&ism=" + StudentInfo.Current.IsMatured + "&isqu=" + isqu + "");
            }
            else
            {
                //失败
                Message.Send(10010);
            }
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    //判断用户密码是否为空
        //    if (this.txtsf.Text == "" || this.txtpwd.Text == "")
        //    {
        //        Message.Send(10036);
        //        return;
        //    }
        //    //判断验证码是否正确
        //    if (Convert.ToString(Session["RBVerifiCode"]).ToLower() != this.VerifCode.Text.ToLower())
        //    {
        //        Message.Send(10037);
        //        return;
        //    }
        //    //判断密码是否正确
        //    if (!StudentInfo.Current.ToLogin(this.txtsf.Text.ToLower(), BaseLibrary.ConvertToMD5(this.txtpwd.Text.ToLower())))
        //    {
        //        Message.Send(10038);
        //        return;
        //    }
        //    else
        //    {
        //        if (StudentInfo.Current.IsDelete)
        //        {
        //            //已被删除
        //            Message.Send(10039);
        //        }
        //        else
        //        {
        //            //登录成功
        //            Message.Send(10003);
        //        }
        //    }
        //}
    }
}